from datetime import date
import json

from django.conf import settings
from rest_framework import serializers
from rest_framework_simplejwt.tokens import RefreshToken
from rest_framework_simplejwt.settings import api_settings

from apps.common.account.models import Account
from apps.pollution.models import Category, Question, Answer
from apps.pollution.utils import upload_file_to_oss

file_domain = settings.FILE_DOMAIN


class QuestionSerializer(serializers.ModelSerializer):

    def to_representation(self, instance):
        ret = super().to_representation(instance)
        return ret

    class Meta:
        model = Question
        fields = "__all__"


class AnswerSerializer(serializers.ModelSerializer):
    answer = serializers.JSONField(required=True)

    def to_representation(self, instance):
        ret = super().to_representation(instance)
        ret['images_full_path'] = []
        if instance.images:
            # ret['images_full_path'] = [f"https://humen-pollution.oss-cn-shenzhen.aliyuncs.com/{path}" for path in instance.images]
            ret['images_full_path'] = [f"{file_domain}{path}" for path in instance.images]
        return ret

    def create(self, validated_data):

        instance = Answer.objects.create(**validated_data)

        # 在账号表新建或更新公司信息
        try:
            company = Account.objects.get(username=validated_data['company_name'])
            company.realname = validated_data['people_name']
            company.telephone = validated_data['telephone']
            company.address = validated_data['address']
            company.is_write = True
            company.save()
        except Account.DoesNotExist:
            Account.objects.create(username=validated_data['company_name'], realname=validated_data['people_name'],
                                   telephone=validated_data['telephone'], address=validated_data['address'],
                                   is_company=1, is_write=1)
        return instance

    class Meta:
        model = Answer
        fields = "__all__"


class CompanySerializer(serializers.ModelSerializer):

    def to_representation(self, instance):
        ret = super().to_representation(instance)
        ret['company_name'] = instance.username
        ret['people_name'] = instance.realname
        return ret

    class Meta:
        model = Account
        fields = ['id', 'address', 'telephone', 'batch_num']


class AnswerListSerializer(serializers.ModelSerializer):
    created_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M", required=False, read_only=True)

    def to_representation(self, instance):
        ret = super().to_representation(instance)
        ret['images_full_path'] = []
        if instance.images:
            # ret['images_full_path'] = [f"https://humen-pollution.oss-cn-shenzhen.aliyuncs.com/{path}" for path in instance.images]
            ret['images_full_path'] = [f"{file_domain}{path}" for path in instance.images]
        return ret

    class Meta:
        model = Answer
        fields = "__all__"
